import logging
from offline_calc.handler import OfflineCalculationHandler
from django.conf import settings
from common.cp.maestro.product_event_handler import (
    game_aggregate_report_handler,
    lottery_aggregate_report_handler,
    bet_user_analysis_report_handler
)
from event_rule.cp.product_event_rule.game_aggregate_report import GAME_AGGREGATE_REPORT
from event_rule.cp.product_event_rule.lottery_aggregate_report import LOTTERY_AGGREGATE_REPORT
from event_rule.cp.product_event_rule.dwc_bet_user_analysis import DWC_BET_USER_ANALYSIS_CONFIG
from event_rule.cp.product_event_rule.lottery_bet_user_analysis import LOTTERY_BET_USER_ANALYSIS_CONFIG

from common.const import INTERVAL_TYPE_MAPPER

_LOGGER = logging.getLogger(__name__)


class Command(OfflineCalculationHandler):
    def __init__(self):
        super(Command, self).__init__()
        self.index_list = settings.CP_INDEX
        self.event_handler_mapper = {
            'dwc_bet_user_analysis': {'func': bet_user_analysis_report_handler, 'rule': DWC_BET_USER_ANALYSIS_CONFIG},
            'lottery_bet_user_analysis': {'func': bet_user_analysis_report_handler,
                                          'rule': LOTTERY_BET_USER_ANALYSIS_CONFIG},
            'dwc_aggregate': {'func': game_aggregate_report_handler, 'rule': GAME_AGGREGATE_REPORT},
            'lottery_aggregate': {'func': lottery_aggregate_report_handler,
                                  'rule': LOTTERY_AGGREGATE_REPORT},
        }

    def add_arguments(self, parser):
        super(Command, self).add_arguments(parser)

    def get_event_rule_and_handler(self, handler_type):
        return (self.event_handler_mapper.get(handler_type, {}).get('rule'),
                self.event_handler_mapper.get(handler_type, {}).get('func'))

    def handle(self, **kwargs):
        super(Command, self).handle(**kwargs)
        merchant = kwargs['merchant']
        target_event = kwargs['target_event']
        self.index_list = self.index_list if not merchant else [index for index in self.index_list if index == merchant]

        for index in self.index_list:
            for handler_type in self.event_handler_mapper:
                if target_event and target_event != handler_type:
                    continue
                event_rule, event_handler = self.get_event_rule_and_handler(handler_type)
                interval_type_list = event_rule.get('interval_type_list', [])
                for interval_type_value in interval_type_list:
                    window_list = self.get_window_interval(interval_type_value, event_rule.get('day_to_calc', [0]))
                    for window in window_list:
                        interval_type = INTERVAL_TYPE_MAPPER[interval_type_value]
                        if not self.check_merchant_start_time(interval_type, index, window):
                            continue
                        if event_handler and event_rule:
                            print(index, handler_type, window)
                            try:
                                self.pool.apipe(self.start_execution, event_handler, interval_type, handler_type,
                                                self.base_time, index, window,
                                                dict(interval_type=interval_type, base_time=self.base_time,
                                                     last_window=window, event_name=handler_type, event_rule=event_rule,
                                                     index=index))
                            except Exception as e:
                                _LOGGER.exception('ES Create Report Error (%s Report) %s', handler_type, e)
        self.pool.close()
        self.pool.join()
